package org.sgroup.imenu.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.sgroup.imenu.bo.Category;
import org.sgroup.imenu.bo.Item;
import org.sgroup.imenu.util.CData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.R.integer;
import android.app.Activity;
import android.content.Context;


public class IMenuXmlSerialize {
	private List<Category> categories;
	private String dataFile="Data.xml";
	private Context context;
	
	public List<Category> getCategories()
	{
		return this.categories;
	}
	public IMenuXmlSerialize(Context context)
	{
		this.context = context;
	}
	public void LoadDatabase()
	{   CData cData = new CData(context);
	//String data = ReadConfig();
	    String data = cData.ReadInnerStorage(dataFile);
		Document doc = XmlDocument.LoadXml(data);
		NodeList nodelist = XmlDocument.selectNodes(doc, "/Data/Category");// doc.getChildNodes();
		//Initialize categories
		this.categories = new ArrayList<Category>();
		if(nodelist ==null) return;
		//Loop Parent categories
		for(int i=0;i<nodelist.getLength();i++)
		{
			Category c = new Category();
			Element e = (Element)nodelist.item(i);
			
			String val = e.getAttribute("ID");
			if(val != null && val !="")
				c.ID = Integer.parseInt(val);
			val = e.getAttribute("Name");
			c.Name = val;
			val = e.getAttribute("Description");
			c.Description = val;
			val = e.getAttribute("ImageFile");
			c.ImageFile = val;
			c.childs = new ArrayList<Category>();
			this.categories.add(c);
			//Loop child categories
			NodeList childcats = XmlDocument.selectNodes(e, "Category");//nodelist.item(i).getChildNodes();
			for(int j = 0;j<childcats.getLength();j++)
			{
				Category c1 = new Category();
				e = (Element)childcats.item(j);
				
				val = e.getAttribute("ID");
				if(val != null && val !="")
					c1.ID = Integer.parseInt(val);
				val = e.getAttribute("Name");
				c1.Name = val;
				val = e.getAttribute("Description");
				c1.Description = val;
				val = e.getAttribute("ImageFile");
				c1.ImageFile = val;
				c1.items = new ArrayList<Item>();
				c.childs.add(c1);
				
				//Loop items
				NodeList items = XmlDocument.selectNodes(e, "Item");//childcats.item(j).getChildNodes();
				for(int k=0;k<items.getLength();k++)
				{
					Item item = new Item();
					e = (Element)items.item(k);
					
					val = e.getAttribute("ID");
					if(val != null && val !="")
						item.ID = Integer.parseInt(val);
					item.CategoryID = c1.ID;

					val = e.getAttribute("Name");
					item.Name = val;
					val = e.getAttribute("Description");
					item.Description = val;
					val = e.getAttribute("ImageFile");
					item.ImageFile = val;
				
					val = e.getAttribute("Price");

					if(val !=null)
						item.Price = Double.parseDouble(val);

					c1.items.add(item);
				}
			}
		}
		
	}
	
	private String ReadConfig()
    {
    	try{	
	    	InputStream is = context.getAssets().open(dataFile);	
	        // We guarantee that the available method returns the total
	        // size of the asset...  of course, this does mean that a single
	        // asset can't be more than 2 gigs.
	        int size = is.available();	
	        // Read the entire asset into a local byte buffer.
	        byte[] buffer = new byte[size];
	        is.read(buffer);
	        is.close();	
	        // Convert the buffer into a string.
	        String text = new String(buffer);
	        return text;
    	}
    	catch (IOException e)
    	{
    		return "";
    	}
    }
}
